<?php

namespace app\admin\controller;

use think\Db;
use my\Security;
use think\Env;
class SmsMass extends Base {
    public $SmsTyep=array();
    public function _initialize()
    {
        ////// is_p  1有附加值，0没有
        ////// ****数组下标与ID必须一致****
        $this->SmsTyep=array(
            1=>array(
                'tyep'=>'OPEN_NOTICE_HF',
                'id'=>1,
                'name'=>'模板一',
                'content'=>'【小马出行】好消息,它来了,小马出行定于8.4下午4点正式上线美团打车,海量订单,奖励多,赶紧打开司机端接单吧。客服电话4001233166回T退订',
                'is_p'=>0,
            ),
            2=>array(
                'tyep'=>'DRIVER_JOIN_CONCAT',
                'id'=>2,
                'name'=>'模板二',
                'content'=>'欢迎加入小马出行，小马出行携手车企同行，为司机师傅们，提供配套服务，有多款车源对外出租哦。请联系车队负责人{电话}微信同号',
                'is_p'=>1,
                'parameter'=>array(
                    array(
                        'name'=>'电话',
                        'parameter'=>'leaderPhone'
                    )
                )
            ),
            3=>array(
                'tyep'=>'DRIVER_DOWN_PATH_MT',
                'id'=>3,
                'name'=>'模板三',
                'content'=>'司机师傅请下载小马聚出行，下载地址：https://qcs.dianping.com/steam/call/call.html?source=tenant&showQrcode=0回T退订',
                'is_p'=>0
            )
            );
        $this->assign('SmsTyep',$this->SmsTyep);
    }

    //积分规则设置
    public function index(){
        if(input('post.')){
            $data=input('post.');
            $data['all_phone']=1;
            $phone=array();

            if($data['type']==1){
                $where['register_city']=$data['city'];
                $phone = Db::name('user')->where($where)->field('user_phone AS phone')->select();
            }else{
                //查询该司机是否在白名单 - 测试名单内
                $whiteList = Db::name('white_list')->order('id desc')->find();
                if (!empty($data['all_phone'])) {
                    if ($data['all_phone'] == 1 && !empty($whiteList['driver_str'])) {        //正式司机
                        $where=' and a.driver_id not in('.$whiteList['driver_str'].')';
                    } elseif ($data['all_phone'] == 2 && !empty($whiteList['driver_str'])) {   //测试司机
                        $where=' and a.driver_id in('.$whiteList['driver_str'].')';
                    }
                }

                $sql="SELECT a.driver_phone AS phone FROM xm_driver_base a WHERE a.driver_id in( SELECT b.driver_id FROM xm_driver_info b WHERE b.work_city=".$data['city'].$where." )";
                $phone=Db::query($sql);
            }

            $phoneArr=array();
            foreach($phone as $k=>$v){
                $phoneArr[]=Security::decrypt($v['phone']);
            }

            echo json_encode(array(
                'code'  => 1,
                'msg'   => '',
                'count' => count($phoneArr),
                'data'  => implode(",", $phoneArr)
            ));
            exit;
        }
        $map['parent_id'] = 1;
        $config['provinces'] = db('region')->where($map)->column('region_id,region_name,parent_id');
        $this->assign($config);
        return $this->fetch();
    }

    //获取区域
    public function getRegion()
    {
        $level = input('post.level/d');
        $this->where[$level]['region_id'][1]&&$map = $this->where[$level];
        $map['parent_id'] = input('post.parent_id');
        if(empty($map['parent_id']) || !in_array($level,array(3,4))){
            return json(array('status'=>0,'msg'=>'参数缺失'));
        }
        $level==4&&$map['region_name'] = array(array('eq',"市辖区"),array('like',"%县%"),array('like',"%市%"),array('like',"%旗%"),'or');
        $region = Db::name('region')->where($map)->field('region_id,region_name,parent_id,region_code')->select();
        return json(array('status'=>1,'data'=>$region));
    }
    /**
     * 获取乡镇的code
     * @param string
     * @param int
     * @return string 成功时返回
     */
    public function getTown(){
        $parent_id = input('post.parent_id');//县的region_id
        $res = Db::table('xm_region_town t')->join(['xm_region'=>'r'],'r.region_code=t.region_code')->where(['r.region_id'=>$parent_id])->field('t.*')->order('t.town_id desc')->select();
        return json(array('status'=>1,'data'=>$res));
    }
    public function postSmsMass(){

        $phoneArr=input('post.phoneArr');
        //java接口
        //$url='http://sms.xiaomachuxing.com:8888/sms/send';//外网
        $url='http://192.168.0.58:8888/sms/send';//内网
        //参数 {"template":"OPEN_NOTICE_HF","phone":"18305540289,15205603921,17333201319"}
        //OPEN_NOTICE_HF 推广
        //DRIVER_JOIN_CONCAT 带车队负责人电话
        $type='OPEN_NOTICE_HF';
        $molSmsTyep=input('post.molSmsTyep');
        $SmsTyep=$this->SmsTyep[$molSmsTyep];

        $post_param['phone']=$phoneArr;
        $post_param['template']=$SmsTyep['tyep'];

        if($SmsTyep['is_p']==1){
            $smsData=$_POST['smsData'];
            foreach($SmsTyep['parameter'] as $k=>$v){
                foreach($smsData as $k1=>$v1){
                    if($k1==$v['parameter']){
                        $post_param[$v['parameter']]=$v1;
                    }
                }
            }
        }

        //$phoneArr='18305540289,18556575623,13956401444';
        echo json_encode(array('status'  => 1,'msg'   => '发送成功'));
        /*echo "<pre>";
        print_r($post_param);*/
        exit;

        //curl请求

        /*$res = $this->httpsRequest($url, json_encode($post_param));
        $return_data = json_decode($res,true);

        if($return_data['success']==1){
            echo json_encode(array('status'  => 1,'msg'   => '发送成功'));
        }else{
            echo json_encode(array('status'  => 0,'msg'   => $return_data['success']));
            //return json(array('status'=>0,'msg'=>$return_data['success']));
        }*/

        //echo json_encode(array('status'  => 1,'msg'   => '发送成功'));
        exit;
    }

    function httpsRequest($url, $data = null) {
        $curl = curl_init ();
        curl_setopt ( $curl, CURLOPT_URL, $url );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );

        curl_setopt ( $curl, CURLOPT_POST, 1 );
        curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
        curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt ( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );

        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt ( $curl, CURLOPT_HTTPHEADER, "Content-Type: application/json" );

        curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: application/json; charset=utf-8','Content-Length: '.strlen($data)));

        $output = curl_exec ( $curl );
        curl_close ( $curl );
        return $output;
    }

}
